package grailsforum

class UsersController {

    def login() {

        if (session.user){
            redirect(controller: 'test', action: 'index')
        }
    }

    def logout(){

        session.user = null
        redirect(controller: 'users', action: 'login')
    }

    def validate(){

        def user = User.findByEmail((String)params.email);

        if (user && user.password == params.password){

            session.user = user;
            redirect(controller: 'courses', action: 'index')

        }
        else{

            flash.message = message(code: 'forum.signin.error')
            render(view: 'login')
        }
    }

    def myPosts(){

        [posts : User.get(session.user.id).posts]
    }

    def myThreads(){

         [posts: Post.findAll({ user.id == session.user.id && position == 0})]
    }
}
